// ---------------------------------------------------------------------
//
// -- 数字组件
// -- author £
//
// ---------------------------------------------------------------------

cc.Class({
    extends: cc.Component,

    properties: {
        atlas: cc.SpriteAtlas,
        preName: '',
        numW: 0, 
        gap: 0,
        align: 0,
        firstLeft: false,
        initStr: {
            default: ''
        },
        totalW: {
            get: function () {
                return this._totalW
            }
        }
    },

    ctor: function () {
        this._numW = 0;
    },

    // use this for initialization
    onLoad: function () {
        if (this.initStr !== '' && this.preName !== '') {
            this.updateNum(this.initStr, null, this.align, this.firstLeft);
        }
    },

    start: function () {
        
    },

    //更新数字
    updateNum: function(num, pre, align, firstLeft) {
        if (align === undefined || align === null)
            align = 0;

        pre = pre || this.preName;
        align = align || this.align;
        firstLeft = firstLeft || this.firstLeft;
        this.node.removeAllChildren();
        this.create(pre, num, this.numW, this.gap, align, firstLeft);
    },

    /** 清空 */
    clear: function () {
        this._totalW = 0;
        this.node.removeAllChildren();
    },

    //生成一个数字贴图
    create: function (pre, num, numW, gap, align, firstLeft) {
        let curNode, curSp, curSF,
            numStr = num + '', 
            len = numStr.length, char,
            key, w, i;
            
        if (gap === undefined) gap = 0;
        if (numW === undefined) numW = 0;
        
        let startX, firstX;
        
        this._totalW = 0;
        // this._totalW = (len - 1) * gap + numW * len;
        
        for (i = 0; i < len; ++i) {
            char = numStr.charAt(i);
            
            switch(char) {
                case '.' :
                    char = 'dot';
                    break;
                case ',' :
                    char = 'comma';
                    break;
                case '+' :
                    char = 'plus';
                    break;
                case '-' :
                    char = 'minus';
                    break;
                case ':':
                    char = 'colon';
            }
            
            key = pre + char;
            curSF = this.atlas.getSpriteFrame(key);
            curNode = new cc.Node();
            curSp = curNode.addComponent(cc.Sprite);
            curSp.trim = false;
            curSp.type = cc.Sprite.Type.SIMPLE;
            curSp.sizeMode = cc.Sprite.SizeMode.RAW;
            curSp.spriteFrame = curSF;
            this.node.addChild(curNode);

            if (i === 0) {
                firstX = -curNode.width / 2;
            }

            curNode.x = firstX + this._totalW + curNode.width / 2;
            this._totalW += (curNode.width + ((i === len - 1) ? 0 : gap));
        }

        switch (align) {
            case 0:
                startX = -this._totalW / 2 - firstX;
                break;
            case 1:
                startX = !firstLeft ? 0 : -firstX;
                break;
            case 2:
                startX = firstLeft ? (-this._totalW - 2 * firstX) : (-this._totalW - firstX);
                break;
        }

        for (i = 0; i < len; ++i) {
            curNode = this.node.children[i];
            curNode.x += startX;
        }
    },
    

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
